<?php

function micro_time()
{
   list($usec, $sec) = explode(" ", microtime());
   return ($usec * 1000000 + $sec * 1000000);
}

$user = 'root';
$pass = '8zDNBNooQxbn';
$dsn  = 'mysql:host=14.18.204.246;port=3306;dbname=test';
try {
    $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
    echo "连接成功\n";

    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    echo "set attr\n";

    // $count = $dbh->exec("insert into user(name)values('asdf')");
    // echo "exec\n";
    // var_dump($count);

    $stmt = $dbh->query('select * from user limit 1');
    echo "query\n";
    $rows = $stmt->fetchAll();
    $endtime = micro_time();
    var_dump("时间差:" . ($endtime - $starttime));
    print_r($rows);

    $starttime = micro_time();
    $stmt = $dbh->prepare("select * from user where `name` = ?");
    echo "prepare\n";
    $stmt->bindValue(1, 'test');
    $stmt->execute();  //执行一条预处理语句 .成功时返回 TRUE, 失败时返回 FALSE
    // print_r($stmt->errorInfo());
    // print_r($dbh->errorInfo());
    $rows = $stmt->fetchAll();
    print_r($rows);
} catch (PDOException $e) {
    die("Error!: ".$e->getMessage()."\n");
}